<?php

/**
 * answer actions.
 *
 * @package    docplus.co.za
 * @subpackage answer
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class answerActions extends gfFrontendActions
{

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $offsetId = $request->getParameter("offset_id", "1");
        $this->answers = AnswerTable::getInstance()->retriveByOffsetId($offsetId);

        // fetch the answer comment form
        $this->_getAnswerCommentForm();
    }

    public function executeNew(sfWebRequest $request)
    {
        if (!$this->getUser()->isAuthenticated() || $this->getUser()->getType() != "Doctor")
            die("signin");

        $question = QuestionTable::getInstance()->find($request->getParameter("question_id", null));
        $answer = new Answer();

        if (!is_object($question))
            die("No question id passed");

        $answer->setQuestion($question);

        if ($this->getUser()->isAuthenticated() && $this->getUser()->getType() == "Doctor")
        {
            $answer->setDoctorId($this->getUser()->getDoctor()->getId());
        }

        $this->form = new AnswerForm($answer);

        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    $answer = $this->form->save();
                    $user = $this->getUser()->getDpUser();

                    // log this activity
                    $description = $this->getPartial('common/activity_log', array('answer' => $answer, 'type' => 'answer', 'user' => $user));
                    $this->dispatcher->notify(new sfEvent($this, 'activity.log', array('description' => $description, 'points' => 5, 'user' => $this->getUser()->getDpUser())));

                    $questionUser = $answer->getQuestion()->getUser();
                    
                    $mailer = new gfMailer();
                    $mailer->setTemplate('answer-notification');
                    $mailer->setToEmails($questionUser->getName() . " <" . $questionUser->getEmail() . ">");
                    $mailer->addValues(array(
                        "USER" => $this->getUser()->getDpUser(),
                        "DOCTOR_PUBLIC_URL" => $this->generateUrl("profile_public", array("slug" => $user->getSlug()), true),
                        "DOCTOR_NAME" => $user,
                        "QUESTION_URL" => $this->generateUrl("question_show", array("id" => $question->getId()), true),
                        "QUESTION" => $question->getEnquiry()
                    ));
                    $mailer->send();

                    // send the asker the doctors response
                    $this->getUser()->setFlash('notice', 'Thanks for successfully submitting your invaluable answer :)');

                    echo $answer->getId();
                    exit;
                }
                catch (Exception $e)
                {
                    throw $e;
                    // invoke the event here so we get the error via email
                }
            }
        }
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->answer = AnswerTable::getInstance()->find($request->getParameter("answer_id", null));

        // fetch the answer comment form
        $this->_getAnswerCommentForm();
    }

    /**
     * Executes rate action
     *
     * @param sfRequest $request A request object
     */
    public function executeRate(sfWebRequest $request)
    {
        if ($request->hasParameter("answer_id") && $request->hasParameter("rating_value") && $this->getUser()->isAuthenticated())
        {
            AnswerRatingTable::getInstance()->updateOrCreateByAnswerIdAndRatingValue($request->getParameter("answer_id"), $request->getParameter("rating_value"), $this->getUser()->getDpUser()->getId());
            $this->answer = AnswerTable::getInstance()->findOneById($request->getParameter("answer_id"));
        }
    }

    /**
     * Executes thank action
     *
     * @param sfRequest $request A request object
     */
    public function executeThanks(sfWebRequest $request)
    {
        if ($request->hasParameter("answer_id") && $this->getUser()->isAuthenticated())
        {
            $user = $this->getUser()->getDpUser();            
            $answerThank = AnswerThankTable::getInstance()->deleteOrCreateByAnswerIdAndUserId($request->getParameter("answer_id"), $user->getId());
            $answer = AnswerTable::getInstance()->find($request->getParameter("answer_id", null));
            
             // log this activity
            $description = $this->getPartial('common/activity_log', array('answer' => $answer, 'type' => is_object($answerThank) ? 'thank' : 'unthank', 'user' => $user));
            $this->dispatcher->notify(new sfEvent($this, 'activity.log', array('description' => $description, 'user' => $user)));
        }
    }

    public function executeCommentNew(sfWebRequest $request)
    {
        if (!$this->getUser()->isAuthenticated())
        {
            die("signin");
        }
        else
        {
            $user = $this->getUser()->getDpUser();
        }


        $this->answer = AnswerTable::getInstance()->find($request->getParameter("answer_id", null));
        $answerComment = new AnswerComment();

        if (!is_object($this->answer))
        {
            die("No answer id passed");
        }

        $answerComment->setAnswer($this->answer);

        if ($this->getUser()->isAuthenticated())
        {
            $answerComment->setUserId($user->getId());
        }
        $this->form = new AnswerCommentForm($answerComment);

        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    $answerComment = $this->form->save();

                    // log this activity
                    $description = $this->getPartial('common/activity_log', array('answer' => $this->answer, 'type' => 'answercomment', 'user' => $user));
                    $this->dispatcher->notify(new sfEvent($this, 'activity.log', array('description' => $description, 'user' => $user)));
                    $this->getUser()->setFlash('notice', 'Thanks for successfully submitting your comment :)');
                    echo $answerComment->getId();
                    exit;
                }
                catch (Exception $e)
                {
                    throw $e;
                    // invoke the event here so we get the error via email
                }
            }
        }
    }

    public function executeCommentShow(sfWebRequest $request)
    {
        $this->answerComment = AnswerCommentTable::getInstance()->find($request->getParameter("comment_id", null));
    }

}
